package com.xxx.controller;

import com.xxx.common.Result;
import com.xxx.common.ResultUtil;
import com.xxx.eneity.Team;
import com.xxx.eneity.noticeStatus;
import com.xxx.out.TeamInfo;
import com.xxx.out.TeamMember;
import com.xxx.out.TeamNotice;
import com.xxx.service.TeamService;
import com.xxx.service.UserService;
import com.xxx.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1.0")
public class TeamController {

    @Autowired
    private TeamService teamService;

    @Autowired
    private UserService userService;


    @PostMapping("/createTeam")
    public Result CreateTeam(@RequestBody CreateTeamVo createTeamVo) {
        if (createTeamVo.getUserId() == null) {
            return ResultUtil.error(1016, "用户编号不能为空");
        } else if (createTeamVo.getName() == null || createTeamVo.getName().equals("")) {
            return ResultUtil.error(1017, "队名不能为空");
        } else if (createTeamVo.getContestId() == null) {
            return ResultUtil.error(1018, "赛事编号不能为空");
        }
        // 判断该成员是否已参赛

        boolean memberExist = teamService.isMemberExistContest(createTeamVo.getContestId(), createTeamVo.getUserId());
        if (memberExist) {
            return ResultUtil.error(1020, "您已在该赛事的队伍中");
        }
        // 判断队名是否已经存在
        Boolean teamExist = teamService.isTeamNameExist(createTeamVo.getName(), createTeamVo.getContestId());
        if (teamExist) {
            return ResultUtil.error(1015, "该队伍名称已存在");
        }
        Team teamInfo = teamService.createTeam(createTeamVo);
        return ResultUtil.success(teamInfo);
    }

    @PostMapping("/editTeam")
    public Result editTeam(@RequestBody EditTeamVo editTeamVo) {
        if (editTeamVo.getTeamName() == null || editTeamVo.getTeamName().equals("")) {
            return ResultUtil.error(4017, "队名不能为空");
        } else if (editTeamVo.getTeamId() == null) {
            return ResultUtil.error(4016, "队伍编号不能为空");
        }

        // 判断队名是否存在
        Boolean teamExist = teamService.isTeamNameExist(editTeamVo.getTeamId(), editTeamVo.getTeamName());
        if (teamExist) {
            return ResultUtil.error(4015, "该队伍名称已存在");
        }

        int i = teamService.editTeam(editTeamVo);
        if (i > 0) {
            return ResultUtil.success("修改队伍信息成功");
        } else {
            return ResultUtil.error(4016, "修改队伍信息失败");
        }
    }

    @DeleteMapping("/kickOutMember")
    public Result kickOutMember(@RequestParam Integer userId, @RequestParam Integer teamId){
        int i = teamService.kickOutMember(userId, teamId);
        if (i>0) {
            return ResultUtil.success("踢出队员成功");
        }else {
            return ResultUtil.error(4038, "踢出队伍成员失败");
        }
    }

    @PostMapping("/replaceCaptain")
    public Result replaceCaptain(@RequestParam Integer teamId, @RequestParam Integer userId) {
        // 判断当前队伍的队长是谁
        Boolean isCaptain = teamService.isCaptain(teamId, userId);
        if (isCaptain) {
            return ResultUtil.error(4039, "当前用户已是该队伍的队长");
        }
        int i = teamService.replaceCaptain(teamId, userId);
        if (i > 0) {
            return ResultUtil.success("更换队长成功");
        } else {
            return ResultUtil.error(4040, "更换队长失败");
        }
    }

    @DeleteMapping("/removeTeam")
    public Result removeTeam(@RequestParam Integer teamId) {
        int i = teamService.removeTeam(teamId);
        if (i > 0) {
            return ResultUtil.success("队伍解散成功");
        } else {
            return ResultUtil.error(2002, "队伍解散失败");
        }
    }

    @GetMapping("/getTeamList")
    public Result getTeamList(@RequestParam Integer userId) {
        List<TeamInfo> teamList = teamService.getTeamList(userId);
        return ResultUtil.success(teamList);
    }

    @GetMapping("/getTeamListAsCaptain")
    public Result getTeamListAsCaptain(@RequestParam Integer userId) {
        List<TeamInfo> teamList = teamService.getTeamListAsCaptain(userId);
        return ResultUtil.success(teamList);
    }

    @PostMapping("/exitTeam")
    public Result exitTeam(@RequestParam Integer teamId, @RequestParam Integer userId) {
        Integer i = teamService.exitTeam(teamId, userId);
        if (i > 0) {
            return ResultUtil.success("退出队伍成功");
        } else {
            return ResultUtil.error(4001, "退出队伍失败");
        }
    }


    @GetMapping("/getTeamInfo")
    public Result getTeamInfo(@RequestParam Integer teamId) {
        TeamInfo teamInfo = teamService.getTeamInfo(teamId);
        return ResultUtil.success(teamInfo);
    }

    @GetMapping("/getTeamMembers")
    public Result getTeamMembers(@RequestParam Integer teamId) {
        List<TeamMember> teamMembers = teamService.getTeamMembers(teamId);
        return ResultUtil.success(teamMembers);
    }

    @GetMapping("/getJoinMember")
    public Result getJoinMember(@RequestParam Integer teamId) {
        List<TeamMember> teamMembers =  teamService.getJoinMember(teamId);
        return ResultUtil.success(teamMembers);
    }

    @PostMapping("reviewJoinRequset")
    public Result reviewApplication(@RequestParam boolean advice,@RequestParam Integer teamId,@RequestParam Integer userId){
        return teamService.reviewApplication(advice,teamId,userId);
    }

    @PostMapping("/reviewJoinRequest")
    public Result reviewJoinRequest(@RequestParam Integer userId, @RequestParam Boolean advice, @RequestParam Integer teamId) {
        int result = teamService.reviewJoinRequest(userId, advice, teamId);
        if (result > 0) {
            if (advice) {
                return ResultUtil.success("同意入队操作执行成功");
            } else {
                return ResultUtil.success("拒绝入队操作执行成功");
            }
        } else {
            return ResultUtil.error(1023, "操作失败");
        }
    }


    @PostMapping("/joinTeam")
    public Result joinTeam(@RequestBody JoinTeamVo joinTeamVo) {
        if (joinTeamVo.getUserId() == null) {
            return ResultUtil.error(1021, "用户编号不能为空");
        } else if (joinTeamVo.getTeamId() == null) {
            return ResultUtil.error(1022, "队伍编号不能为空");
        } else if (joinTeamVo.getJoinNote() == null || "".equals(joinTeamVo.getJoinNote())) {
            return ResultUtil.error(1099, "入队宣言不能为空");
        }
        // 判断成员是否已经申请入队或在该赛事的其他队伍中
        boolean memberExist = teamService.isMemberExist(joinTeamVo.getTeamId(), joinTeamVo.getUserId());
        if (memberExist) {
            return ResultUtil.error(1020, "您已提交入队申请或已在该队伍中");
        }
        // 获取用户名
        joinTeamVo.setUserName(userService.getUserInfo(joinTeamVo.getUserId()).getName());
        int result = teamService.addTeamMember(joinTeamVo);
        if (result > 0) {
            return ResultUtil.success("入队申请已提交");
        } else {
            return ResultUtil.error(1019, "发起入队申请失败");
        }
    }

    @PostMapping("/addTeamNotice")
    public Result addTeamNotice(@RequestBody TeamNoticeVo teamNoticeVo) {
        if (teamNoticeVo.getTeamId() == null) {
            return ResultUtil.error(1025, "队伍编号不能为空");
        } else if (teamNoticeVo.getTitle() == null) {
            return ResultUtil.error(1026, "公告标题不能为空");
        }
        int result = teamService.addTeamNotice(teamNoticeVo);
        if (result > 0) {
            return ResultUtil.success("发布队内公告成功");
        } else {
            return ResultUtil.error(1024, "发布队内公告失败");
        }

    }

    @PostMapping("/editTeamNotice")
    public Result editTeamNotice(@RequestBody EditNoticeVo editNoticeVo) {
        if (editNoticeVo.getTitle() == null) {
            return ResultUtil.error(1027, "公告标题不能为空");
        } else if (editNoticeVo.getNoticeId() == null) {
            return ResultUtil.error(1028, "公告编号不能为空");
        }
        int result = teamService.editTeamNotice(editNoticeVo);
        if (result > 0) {
            return ResultUtil.success("修改队内公告成功");
        } else {
            return ResultUtil.error(1029, "修改队内公告失败");
        }
    }

    @DeleteMapping("/removeTeamNotice")
    public Result removeTeamNotice(@RequestParam Integer noticeId) {
        int result = teamService.removeTeamNotice(noticeId);
        if (result > 0) {
            return ResultUtil.success("删除队内公告成功");
        } else {
            return ResultUtil.error(1030, "删除队内公告失败");
        }
    }

    @GetMapping("/getTeamNoticeList")
    public Result getTeamNoticeList(@RequestParam Integer teamId) {
        List<TeamNotice> teamNotices = teamService.getTeamNoticeList(teamId);
        return ResultUtil.success(teamNotices);
    }

    @GetMapping("/getTeamNoticeStatusList")
    public Result getTeamNoticeStatusList(@RequestParam Integer teamId, Integer userId) {
        // 查看队伍公告，包含已读状态
        List<TeamNotice> teamNotices = teamService.getTeamNoticeStatusList(teamId, userId);
        return ResultUtil.success(teamNotices);
    }

    @GetMapping("/getReadStatus")
    public Result getReadStatus(@RequestParam Integer noticeId) {
        // 查看成员阅读状态
        List<noticeStatus> noticeStatusList =  teamService.getReadStatus(noticeId);
        return ResultUtil.success(noticeStatusList);
    }

    @GetMapping("/getTeamInfoList")
    public Result getTeamInfoList(@RequestParam(required = false) Integer contestId) {
        List<TeamInfo> teamInfoList = teamService.getTeamInfoList(contestId);
        return ResultUtil.success(teamInfoList);
    }

    @GetMapping("/getTeamNumber")
    public Result getTeamNumber(@RequestParam Integer contestId) {
        Integer number = teamService.getTeamInfoList(contestId).size();
        return ResultUtil.success(number);
    }

    @GetMapping("/getTeamName")
    public Result getTeamName(@RequestParam Integer contestId, @RequestParam Integer userId) {
        TeamInfo teamInfo = teamService.getTeamName(contestId, userId);
        return ResultUtil.success(teamInfo);
    }

    @PostMapping("/readTeamNotice")
    public Result readTeamNotice(@RequestParam Integer userId, @RequestParam Integer noticeId) {
        // 判读是否已读
        int count = teamService.isReadTeamNotice(userId, noticeId);
        if (count > 0) {
            return ResultUtil.error(4003, "您已阅读该公告");
        }
        int i = teamService.readTeamNotice(userId, noticeId);
        if (i > 0) {
            return ResultUtil.success("阅读队伍公告成功");
        } else {
            return ResultUtil.error(4002, "阅读队伍公告失败");
        }
    }


    @GetMapping("/getOtherUserList")
    public Result getOtherUserList(@RequestParam Integer teamId) {
        List<UserVo> userVoList = teamService.getOtherUserList(teamId);
        return ResultUtil.success(userVoList);
    }

    @PostMapping("/addMember")
    public Result addMember(@RequestParam Integer userId, @RequestParam Integer teamId) {
        int i = teamService.addMember(userId, teamId);
        if (i > 0) {
            return ResultUtil.success("添加成员成功");
        } else {
            return ResultUtil.error(4099, "添加成员失败");
        }
    }
}
